package com.atguigu.gmall.seckill.rpc;

import com.atguigu.gmall.common.config.utils.UserAuthUtil;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.seckill.service.SeckillBizService;
import com.atguigu.gmall.seckill.service.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-26 09:42
 **/
@RequestMapping("/api/inner/rpc/seckill")
@RestController
public class SeckillRpcController {
    @Autowired
    SeckillBizService seckillBizService;

    /**
     * 获取今天参与秒杀的所有商品
     *
     * @return
     */
    @GetMapping("/today/goods")
    public Result<List<SeckillGoods>> getTodaySeckillGoods() {
        // 1.得到今天的日期，例如：2022年-12-26
        String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
        // 2.返回redis中的数据
        List<SeckillGoods> goods = seckillBizService.getTodaySeckillGoods(date);
        return Result.ok(goods);
    }

    /**
     * 获取某个商品详情
     *
     * @param skuId
     * @return
     */
    @GetMapping("/goods/detail/{skuId}")
    public Result<SeckillGoods> getSeckillGoodsDetails(@PathVariable("skuId") Long skuId) {
        SeckillGoods seckillGoods = seckillBizService.getSeckillGoodsDetails(skuId);
        return Result.ok(seckillGoods);
    }

    @GetMapping("/order/{skuId}")
    public Result<OrderInfo> seckillOrderInfo(@PathVariable("skuId") Long skuId) {
        String userId = UserAuthUtil.getUserIdHeader();
        OrderInfo orderInfo = seckillBizService.getSeckillOrderInfo(skuId, userId);
        return Result.ok(orderInfo);
    }
}
